package com.cheyunkeji.er.view.fast_evaluate;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.view.MyRadioGroup;
import com.cheyunkeji.er.view.TabTitle;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Michael on 2017/4/17 13:35.
 */

public class YearAndMonthPickerView extends RelativeLayout {


    @BindView(R.id.tt_switch)
    TabTitle ttSwitch;
    @BindView(R.id.rg_button_list)
    MyRadioGroup rgButtonList;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    private View rootView;

    private int attachedListItemPosition = -1;
    private OnCurrentListItemChecked listener;

    public YearAndMonthPickerView(Context context) {
        super(context);
        initView(context);
    }

    public YearAndMonthPickerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public YearAndMonthPickerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public void setAttachedListItemPosition(int position) {
        this.attachedListItemPosition = position;
    }

    private void initView(Context context) {

        rootView = LayoutInflater.from(context).inflate(R.layout.er_year_and_month_pickview, this, true);
        ButterKnife.bind(this, rootView);

        ttSwitch.hideIndicator();
        ttSwitch.setView(llContent);
        rgButtonList.setOnCheckedChangeListener(new MyRadioGroup.OnCheckedChangeListener() {

                                                    @Override
                                                    public void onCheckedChanged(MyRadioGroup group, int checkedId) {
                                                        if (YearAndMonthPickerView.this.listener != null && YearAndMonthPickerView.this.attachedListItemPosition != -1 && -1 != getCheckedMonth()) {

                                                            listener.notifyListClearOthersCheck(YearAndMonthPickerView.this.attachedListItemPosition,getCheckedMonth());
                                                            Log.e("===yearview", "onCheckedChanged: " + checkedId);
                                                        }
                                                    }
                                                }

//        new OnMonthCheckedChangeListener(attachedListItemPosition,listener)
        );
    }

    private int getCheckedMonth() {

        int month = -1;
        switch (rgButtonList.getCheckedRadioButtonId()) {
            case R.id.rb_month1:
                month = 1 ;
                break;
            case R.id.rb_month2:
                month = 2 ;
                break;
            case R.id.rb_month3:
                month = 3 ;
                break;
            case R.id.rb_month4:
                month = 4 ;
                break;
            case R.id.rb_month5:
                month = 5 ;
                break;
            case R.id.rb_month6:
                month = 6 ;
                break;
            case R.id.rb_month7:
                month = 7 ;
                break;
            case R.id.rb_month8:
                month = 8 ;
                break;
            case R.id.rb_month9:
                month = 9 ;
                break;
            case R.id.rb_month10:
                month = 10 ;
                break;
            case R.id.rb_month11:
                month = 11 ;
                break;
            case R.id.rb_month12:
                month = 12 ;
                break;

            default:
                break;
        }

        return month ;
    }


    /**
     * 设置title
     *
     * @param titleDesc
     */
    public void setTitle(String titleDesc) {

        if (!TextUtils.isEmpty(titleDesc)) {
            ttSwitch.setTitle(titleDesc);
        }
    }

    /**
     * 清除RadioGroup当前选中状态
     */
    public void clearCheck() {
        rgButtonList.clearCheck();
    }

    public void setClearOthersCheckListener(OnCurrentListItemChecked listener) {
        this.listener = listener;
    }

    public void setMyOnCheckedChangeListener(MyRadioGroup.OnCheckedChangeListener listener) {
        rgButtonList.setOnCheckedChangeListener(listener);
    }

//    public void setCheckedToMonth12(){
//        rgButtonList.setCheckWithoutNotif(R.id.rb_month12);
//    }

    public interface OnCurrentListItemChecked {

        void notifyListClearOthersCheck( int attachedToListItemPosition , int selectedMonth );

    }

}
